#!/bin/sh

test_description="test create ref review"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
	REPO_ID=$(find_repo | jq .id)
	if [ -n $REPO_ID ]; then
		delete_repo > delete_repo.resp 2>&1
		test_expect_code 0
	fi
	create_repo > create_repo.resp 2>&1
	REPO_ID=$(cat create_repo.resp) &&
	add_member $WRITE_LEVEL $another_user_external &&
	init_repo > init_repo.resp &&
	test_expect_code 0
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
	echo "This is the first commit" > "$REPO_DIR"/README &&
	git -C "$REPO_DIR" add README &&
	git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	echo "This is the second commit" >> "$REPO_DIR"/README &&
	git -C "$REPO_DIR" add README &&
	git -C "$REPO_DIR" commit -m "second commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" tag v1.0 &&
	git -C "$REPO_DIR" push origin master > test-2 2>&1 &&
	git -C "$REPO_DIR" push --tags &&
	test_expect_code 0
'

test_expect_success "[test-3] reviewer can not create ref review" '
	add_member $READ_LEVEL $another_user_external &&
	git -C "$REPO_DIR" checkout -b dev &&
	git -C "$REPO_DIR" push -u origin dev > test-3 2>&1
	grep "failed to push some refs to " test-3
'

test_expect_success "[test-4] developer can push ref normally" '
	add_member $WRITE_LEVEL $another_user_external &&
	git -C "$REPO_DIR" push -u origin dev > test-4 2>&1 &&
	grep "set up to track" test-4
'

test_expect_success "[test-5] developer create branch will create ref review" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" checkout -b feat &&
	git -C "$REPO_DIR" push -u origin feat > test-5 2>&1 &&
        grep "A ref review has been created" test-5
'

test_expect_success "[test-6] developer create tag will create ref review" '
	git -C "$REPO_DIR" tag v2.0 &&
	git -C "$REPO_DIR" push --tags > test-6 2>&1 &&
        grep "A ref review has been created" test-6
'

test_expect_success "[test-7] developer delete branch will create ref review" '
	git -C "$REPO_DIR" push origin :dev > test-7 2>&1 &&
        grep "A ref review has been created" test-7
'

test_expect_success "[test-8] developer delete tag will create ref review" '
	git -C "$REPO_DIR" push origin :v1.0 > test-8 2>&1 &&
        grep "A ref review has been created" test-8
'

test_expect_success "[test-9] developer reset branch will create ref review" '
	git -C "$REPO_DIR" push origin --force HEAD~:master > test-9 2>&1 &&
        grep "A ref review has been created" test-9
'

test_expect_success "[test-10] developer reset tag will create ref review" '
	git -C "$REPO_DIR" push origin --force HEAD~:v1.0 > test-10 2>&1 &&
        grep "A ref review has been created" test-10
'

test_expect_success "[test-11] only matched ref will create ref review" '
	update_branch_pattern $REPO_ID "dev**" &&
	git -C "$REPO_DIR" push origin :dev HEAD:dev2 HEAD:de > test-11 2>&1
	grep "A ref review has been created" test-11 &&
	grep "new branch" test-11
'

test_expect_success "[test-12] only matched ref will create ref review" '
	update_branch_pattern $REPO_ID "feat*" &&
	git -C "$REPO_DIR" push origin HEAD:fea HEAD:feat HEAD:feat1 HEAD:feat/ure > test-12 2>&1
	grep "A ref review has been created" test-12 &&
	grep "HEAD -> feat/ure" test-12 &&
	grep "HEAD -> fea" test-12
'

test_expect_success "[test-13] create ref review and code review" '
	echo "make commit" >> "$REPO_DIR"/README &&
        git -C "$REPO_DIR" add README &&
        git -C "$REPO_DIR" commit -m "make commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin -o review=new HEAD:fea HEAD:feat2 > test-13 2>&1
	grep "A ref review has been created" test-13 &&
	grep "Code review" test-13
'

test_expect_success "[test-14] code change will not create ref review" '
	echo "[test-14] code change will create code review" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "code change will create code review" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin HEAD:de > test-14 2>&1 &&
	grep "HEAD -> de" test-14
'

test_expect_success "[test-15] default assignees are correct" '
	update_default_assignees $REPO_ID $another_user_external &&
	update_branch_pattern $REPO_ID "dev**" &&
	git -C "$REPO_DIR" push origin HEAD:devvv > test-15 2>&1 &&
	grep "A ref review has been created" test-15
	query_ref_review $REPO_ID 11 > query &&
	reviewers=$(cat query | jq .reviewers) &&
	echo $reviewers | grep $another_user_external
'

test_expect_success "[test-16] can not create identical ref review" '
	git -C "$REPO_DIR" push origin HEAD:devvv > test-16 2>&1
	grep "failed to push some refs to" test-16
'

test_expect_success "[test-17] clean up" '
  delete_repo "$REPO_ID" > test-17 2>&1
'

test_done